package highscore;

import java.text.SimpleDateFormat;

import javax.xml.soap.*;

import play.Logger;
import models.QuizGame;
import models.QuizUser;

public class HighScoreClient {
    
    
    public static String SentGame(QuizGame game) {
        
        try {
        
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            String url = "http://playground.big.tuwien.ac.at:8080/highscore/PublishHighScoreService";
                    
            SOAPMessage soapResponse = soapConnection.call(createMessage(game,true), url);
            String UUID = soapResponse.getSOAPBody().getChildNodes().item(0).getTextContent();
            Logger.info("UUID:"+UUID);
               
            soapConnection.close();
			return UUID;
        
        } catch (Exception e ) {
            Logger.warn("Sent Game went wrong");
			return null;
        }
        
    }
    
    private static SOAPMessage createMessage(QuizGame game, Boolean test) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        
        envelope.addNamespaceDeclaration("data", "http://big.tuwien.ac.at/we/highscore/data");

        SOAPBody soapBody = envelope.getBody();
        
        SOAPElement root = soapBody.addChildElement("HighScoreRequest","data");
        
        root.addChildElement("UserKey", "data").addTextNode("rkf4394dwqp49x");
        
        
        SOAPElement users = root.addChildElement("quiz").addChildElement("users");
        
        for(QuizUser qu: game.getPlayers() ) {
            SOAPElement user = users.addChildElement("user");
            
            if(game.getWinner().equals(qu)) {
                user.addAttribute(envelope.createName("name"), "winner");
            } else {
                user.addAttribute(envelope.createName("name"), "loser");
            }
            
            if(qu.getGender()!=null){
                user.addAttribute(envelope.createName("gender"), qu.getGender().toString());
            }else{
                if(test)user.addAttribute(envelope.createName("gender"), "female");
            }
                        
            user.addChildElement("password").addTextNode("");
            
            if(qu.getFirstName()!=null) {
                user.addChildElement("firstname").addTextNode(qu.getFirstName());
            }else {
                if(test) user.addChildElement("firstname").addTextNode("Max");
            }
            if(qu.getLastName()!=null) {
                user.addChildElement("lastname").addTextNode(qu.getLastName());
            }else{
                if(test) user.addChildElement("lastname").addTextNode("Maxus");
            }
            if(qu.getBirthDate()!=null) {
                SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
                user.addChildElement("birthdate").addTextNode(DATE_FORMAT.format(qu.getBirthDate()));
            }else{
                if(test) user.addChildElement("birthdate").addTextNode("1956-05-07");
            }
            
        }
        
        soapMessage.saveChanges();
        return soapMessage;   
    }
    
    

}
